package com.test.restaurant.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.test.restaurant.dao.DishDAO;
import com.test.restaurant.pojo.Dish;
import com.test.restaurant.service.DishService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 徒有琴
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishDAO dishDAO;

    @Autowired
    private HttpSolrClient solrClient;

    @Override
    public void initSolr() {
        List<Dish> dishes = dishDAO.getDishList();
        try {
            for (Dish dish : dishes) {
                SolrInputDocument doc = new SolrInputDocument();
                //这个field是在solr core中配置过的
                doc.addField("id", dish.getId());
                doc.addField("dish_name", dish.getName());
                doc.addField("dish_price", dish.getPrice());
                doc.addField("dish_image", dish.getImage());
                solrClient.add(doc);
            }
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Dish> search(String keywords) {
        List<Dish> dishList = new ArrayList<>();
        SolrQuery query = new SolrQuery("item_keywords:" + keywords); //定义查询内容
        //query.setStart(0);//起始页
        //query.setRows(3);//每页显示数量
        query.setSort("id", SolrQuery.ORDER.asc);//排序
        query.setHighlight(true);//设置高亮
        query.addHighlightField("dish_name");//设置高亮字段
        // 设置高亮的样式
        query.setHighlightSimplePre("<font color='red'>");
        query.setHighlightSimplePost("</font>");
        try {
            QueryResponse rsp = solrClient.query(query);
            Map<String, Map<String, List<String>>> highLightings = rsp.getHighlighting();
            SolrDocumentList results = rsp.getResults();
            for (SolrDocument doc : results) {
                Dish dish = new Dish();
                dish.setId(Integer.parseInt(doc.get("id").toString()) );
                Map<String, List<String>> highLights = highLightings.get(doc.get("id"));
                String name=(String) doc.get("dish_name");
                if (highLights != null) {
                    List<String> keys = highLights.get("dish_name");
                    if (keys != null && keys.size() > 0) {
                        name=keys.get(0);
                    }
                }
                dish.setName(name);
                dish.setImage((String) doc.get("dish_image"));
                dish.setPrice(Integer.parseInt(doc.get("dish_price").toString()));
                dishList.add(dish);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return dishList;
    }
}
